package com.kdtech.analyse.video;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

public class LztvnetAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		// http://www.lztvnet.com/video.aspx?ID=76995
		//http://liuzhou.cutv.com/video.aspx?ID=77424
		
		String[] regex = { "http://www.lztvnet.com/video.aspx\\?ID=[0-9]*"
				,"http://liuzhou.cutv.com/video.aspx\\?ID=[0-9]*"
		};

		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = 0l;

		Document doc = Jsoup.parse(html);
		/*
		 * 解析标题
		 */
		title = doc.select("div.con_hot_video>h2").text();
		/*
		 * 解析时间
		 */
		String text = doc.select("div.con_video_info").text();
		date = DateUtils.matchDate(text);
		/*
		 * 解析播放次数
		 */
		int pos = text.indexOf("播放次数：");
		if (pos != -1) {
			text = text.substring(pos + "播放次数：".length());
			pos = text.indexOf("评论数：");
			if (pos != -1) {
				text = text.substring(0, pos);
				try {
					int parseInt = Integer.parseInt(text);
					video.setClickNum(parseInt);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
			}
		}
		desc = doc.select("#con_topic_txt1").text();
		if(desc.endsWith(" 展开")){
			desc = StringUtils.substringBeforeLast(desc, " 展开");
		}
		video.setContent(desc);
		video.setUpdateUrl(url);
		video.setTitle(title);
		video.setDate(date);
		return video;
	}

	private final static Pattern P_PATH = Pattern.compile("/[0-9]{14}");

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String html = responseToURL.getHtml();
					Document doc = Jsoup.parse(html);
					String text = doc.select("div.con_video_info").text();
					/*
					 * 解析播放次数
					 */
					int pos = text.indexOf("播放次数：");
					if (pos != -1) {
						text = text.substring(pos + "播放次数：".length());
						pos = text.indexOf("评论数：");
						if (pos != -1) {
							text = text.substring(0, pos);
							try {
								int parseInt = Integer.parseInt(text);
								meta.setClickNum(parseInt);
								return meta;
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
							}
						}
					}
				}

			}

		}
		return null;
	}

	public static void main(String[] args) {
		LztvnetAnalyse a = new LztvnetAnalyse();
		String url = "http://www.lztvnet.com/video.aspx?ID=147967";
		if (a.isDetailPage(url)) {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不匹配规则");
		}

	}
	
}
